//
//  startupCount.m
//  Beat Bin Laden
//
//  Created by How Charles on 11-11-23.
//  Copyright 2011 hust. All rights reserved.
//

#import "startupCount.h"


@implementation StartupCount

/*
 单例模式
 */
static StartupCount* _StartupCount = nil;  
+(StartupCount*)sharedInstance  
{  
	@synchronized([StartupCount class])  
	{  
		if (!_StartupCount)  
			[[self alloc] init];  
		
		return _StartupCount;  
	}  
	
	return nil;  
}  
+(id)alloc  
{  
	@synchronized([StartupCount class])  
	{  
		NSAssert(_StartupCount == nil, @"Attempted to allocate a second instance of a singleton.");  
		_StartupCount = [super alloc];  
		return _StartupCount;  
	}  
	
	return nil;  
}  




///////////////////////////////
///////////////////////////////
///////////////////////////////

#pragma mark export interface

-(void)initArray:(int *)Array Size:(int)size
{
	for (int i=0; i<MAX_STARTUP_ITEM_COUNT; i++) 
	{
		array[i]=Array[i];
	}
}

-(void)initArrayFromFile
{
	int IArray[MAX_STARTUP_ITEM_COUNT];
	for (int i=0; i<MAX_STARTUP_ITEM_COUNT; i++) {
		IArray[i]=0;   ///init to 0
	}
	
	[SecureFileRW readFile:IArray Size:MAX_STARTUP_ITEM_COUNT
					  path:[SecureFileRW UserDataFilePath]];   //read from file
	
	[self initArray:IArray Size:MAX_STARTUP_ITEM_COUNT]; 
}


-(void)saveArrayToFile
{
	[SecureFileRW WriteFile:array Size:MAX_STARTUP_ITEM_COUNT
					   path:[SecureFileRW UserDataFilePath]];
}



-(void)increaseStartupCount
{
	array[STORAGE_ARRAY_ITEM_STARTUP_COUNT]++;
}


-(int)getStartupCount
{
	return array[STORAGE_ARRAY_ITEM_STARTUP_COUNT];
}


-(bool)isShouldShowReview
{
	return (0==(array[STORAGE_ARRAY_ITEM_STARTUP_COUNT]%HINT_USER_STARTUP_COUNT));
}


-(void)doAllWhenStart
{
	[self initArrayFromFile];
	[self increaseStartupCount];
	
	if ([self isShouldShowReview]) {
		[[CloudReview sharedReview] reviewThisApp];
	}
}


-(void)doAllWhenExit
{
	[self saveArrayToFile];
}

#pragma mark end of export interface

///////////////////////////////
///////////////////////////////
///////////////////////////////



@end
